package com.yuyou.fn.settlement.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.settlement.entity.FeeCategory;
import com.yuyou.fn.settlement.entity.RelFeeCategoryIncomeCategory;
import com.yuyou.fn.settlement.mapper.FeeCategoryMapper;
import com.yuyou.fn.settlement.service.IFeeCategoryService;
import com.yuyou.fn.settlement.service.IFeeService;
import com.yuyou.fn.settlement.service.IIncomeCategoryService;
import com.yuyou.fn.settlement.service.IRelFeeCategoryIncomeCategoryService;
import com.yuyou.fn.settlement.vo.FeeCategoryDetailVo;
import com.yuyou.fn.settlement.vo.FeeCategoryTreeMapVo;
import com.yuyou.fn.settlement.vo.FeeCategoryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * FeeCategory 表数据服务层接口实现类
 */
@Service
public class FeeCategoryServiceImpl extends SuperServiceImpl<FeeCategoryMapper, FeeCategory, Long> implements IFeeCategoryService {

    @Resource
    private IFeeService feeService;

    @Resource
    private IIncomeCategoryService incomeCategoryService;

    @Resource
    private IRelFeeCategoryIncomeCategoryService relFeeCategoryIncomeCategoryService;



    @Override
    public FeeCategoryDetailVo findFeeCategoryDetail(Long feeCategoryId) {
        FeeCategoryDetailVo feeCategoryDetailVo=new FeeCategoryDetailVo();
        FeeCategory feeCategory=this.findById(feeCategoryId);
        BeanUtils.copyProperties(feeCategoryDetailVo,feeCategory);
        RelFeeCategoryIncomeCategory relFeeCategoryIncomeCategory=relFeeCategoryIncomeCategoryService.findById(feeCategoryId);
        if (relFeeCategoryIncomeCategory!=null){
            feeCategoryDetailVo.setIncomeCategoryId(relFeeCategoryIncomeCategory.getIncomeCategoryId());
        }
        return feeCategoryDetailVo;
    }

    @Override
    public Page<FeeCategory> findFeeCategory(int pageNo, int pageSize) {
        Example<FeeCategory> example = FeeCategory.createExample();
        example.orderBy("createTime").desc();
        return findPageByExample(example, pageNo, pageSize);
    }

    @Override
    public void saveOrUpdateFeeCategory(Long feeCategoryId, String name, String remark) {
        FeeCategory feeCategory = new FeeCategory();
        feeCategory.setUpdateTime(TimeUtils.now());
        feeCategory.setName(name);
        feeCategory.setRemark(remark);
        if (feeCategoryId == null) {
            feeCategory.setCreateTime(TimeUtils.now());
            feeCategory.setIsBuildIn(0);
            insertSelective(feeCategory);
        } else {
            feeCategory.setFeeCategoryId(feeCategoryId);
            updateSelectiveById(feeCategory);
        }
    }

    @Override
    public void deleteFeeCategory(Long feeCategoryId) {
        FeeCategory feeCategory = findById(feeCategoryId);
        if (feeCategory != null) {
            if (feeCategory.getIsBuildIn() == 1) {
                throw new BusException("系统内置成本类型不能删除");
            }

            Long l = feeService.countByFeeCategory(feeCategoryId);
            if (l > 0) {
                throw new BusException("费用类型已被使用了，不能删除");
            }

            deleteById(feeCategoryId);

        }
    }

    @Override
    public List<FeeCategory> findAllFeeCategory() {
        return findListByExample(FeeCategory.createExample());
    }


    @Override
    @Transactional
    public void saveOrUpdate(FeeCategoryVo feeCategoryVo) {
        if (feeCategoryVo.getParentFeeCategoryId()==null){
            feeCategoryVo.setParentFeeCategoryId(0L);
        }
        FeeCategory feeCategory = new FeeCategory();
        BeanUtils.copyProperties(feeCategory, feeCategoryVo);
        if (feeCategoryVo.getFeeCategoryId() == null) {
            this.insertSelective(feeCategory);
        } else {
            this.updateSelectiveById(feeCategory);
        }
        if (feeCategoryVo.getIncomeCategoryId() != null) {
            relFeeCategoryIncomeCategoryService.deleteById(feeCategory.getFeeCategoryId());
            RelFeeCategoryIncomeCategory relFeeCategoryIncomeCategory = new RelFeeCategoryIncomeCategory();
            relFeeCategoryIncomeCategory.setFeeCategoryId(feeCategory.getFeeCategoryId());
            relFeeCategoryIncomeCategory.setIncomeCategoryId(feeCategoryVo.getIncomeCategoryId());
            relFeeCategoryIncomeCategory.setCreateTime(TimeUtils.now());
            relFeeCategoryIncomeCategory.setUpdateTime(TimeUtils.now());
            relFeeCategoryIncomeCategoryService.insertSelective(relFeeCategoryIncomeCategory);
        }
    }

    @Override
    public List<FeeCategoryTreeMapVo> findTreeCategoryMapVo() {
        Example example=FeeCategory.createExample();
        List<FeeCategory> allFeeCategoryList=this.findAllFeeCategory();
        Map<Long,FeeCategory> allFeeCategoryTreeMap =  BeanUtils.toMap(allFeeCategoryList, "feeCategoryId");
        return toTreeCategoryMapVo( allFeeCategoryList , allFeeCategoryTreeMap, 0L);
    }


    private List<FeeCategoryTreeMapVo> toTreeCategoryMapVo(List<FeeCategory> allFeeCategoryList , Map<Long, FeeCategory> allFeeCategoryTreeMap, Long parentId) {
        List<FeeCategoryTreeMapVo> list  = new ArrayList<FeeCategoryTreeMapVo>(10);
        for(FeeCategory feeCategory :  allFeeCategoryList ){
            if(parentId.equals(feeCategory.getParentFeeCategoryId())){
                FeeCategoryTreeMapVo feeCategoryTreeMapVo = new FeeCategoryTreeMapVo();
                BeanUtils.copyProperties(feeCategoryTreeMapVo,feeCategory);
                feeCategoryTreeMapVo.setValue(feeCategory.getFeeCategoryId().toString());
                feeCategoryTreeMapVo.setLabel(feeCategory.getName());
                list.add(feeCategoryTreeMapVo);
                List<FeeCategoryTreeMapVo> childrenList = toTreeCategoryMapVo(allFeeCategoryList, allFeeCategoryTreeMap, feeCategory.getFeeCategoryId());
                if(childrenList.size() > 0 ){
                    feeCategoryTreeMapVo.setChildren(childrenList);
                }
            }
        }



        return list;
    }

    @Override
    public List<FeeCategoryTreeMapVo> findSubTreeByParentId(Long parentFeeCategoryId) {
        Example example=FeeCategory.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("parentFeeCategoryId",parentFeeCategoryId);
        List<FeeCategory> feeCategoryList=this.findListByExample(example);
        Map<Long,FeeCategory> allFeeCategoryTreeMap =  BeanUtils.toMap(feeCategoryList, "feeCategoryId");
        return toTreeCategoryMapVo( feeCategoryList , allFeeCategoryTreeMap, parentFeeCategoryId);
    }

    @Override
    public FeeCategory findByParentId(Long parentId) {
        Example example=FeeCategory.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("parentFeeCategoryId",parentId);
        return findOneByExample(example);
    }
}